//Copyright (c) 2010 Jimmy Merari - jim.merari@gmail.com

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.


namespace Cinovasi.Scriptagility
{
    using System;
    using System.Web;
    using Jayrock.Json;
    using Jayrock.JsonRpc;
    using Jayrock.JsonRpc.Web;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Security.Roles;

    public class ScriptInfoJSON
    {
        public int ScriptId;
        public string ScriptName;
        public int PlacementInt;
        public int Enabled;
        public string Script;
        public int EnableTokenReplace;
        public int ModuleId;
        public int ScriptOrder;
        public string ContentType;

        public ScriptInfoJSON()
        {
            ScriptName = "";
            Script = "";
            ContentType = "";
        }
    }


    public partial class ScriptagilityApi : JsonRpcHandler
    {

        [JsonRpcMethod("getScript")]
        public ArrayList GetScripts(int moduleId)
        {
            VerifyAdmin();
            ScriptagilityController ctrl = new ScriptagilityController();
            ArrayList org = ScriptInfo.GetScriptByModuleId(moduleId);
            ArrayList client = new ArrayList();
            foreach (ScriptInfo objOrg in org)
            {
                ScriptInfoJSON objConvert = new ScriptInfoJSON();
                objConvert.ScriptId = objOrg.ScriptId;
                objConvert.ScriptName = objOrg.ScriptName;
                objConvert.PlacementInt = objOrg.PlacementInt;
                objConvert.Script = objOrg.Script;
                objConvert.EnableTokenReplace = objOrg.EnableTokenReplace ? 1 : 0;
                objConvert.ModuleId = objOrg.ModuleId;
                objConvert.ScriptOrder = objOrg.ScriptOrder;
                objConvert.ContentType = objOrg.ContentType;

                client.Add(objConvert);
            }
            return client;
        }

        [JsonRpcMethod("saveScript")]
        public bool SaveScript(ScriptInfoJSON info)
        {
            VerifyAdmin();
            ScriptInfo objConvert = ScriptInfo.GetScriptById(info.ScriptId);
            objConvert.ScriptId = info.ScriptId;
            objConvert.ScriptName = info.ScriptName;
            objConvert.PlacementInt = info.PlacementInt;
            objConvert.Script = info.Script;
            objConvert.EnableTokenReplace = info.EnableTokenReplace == 1 ? true : false;
            objConvert.ModuleId = info.ModuleId;
            objConvert.ScriptOrder = info.ScriptOrder;
            objConvert.ContentType = info.ContentType;
            objConvert.SaveScript();
            return true;
        }

        [JsonRpcMethod("deleteScript")]
        public void DeleteScript(int scriptId)
        {
            VerifyAdmin();
            ScriptInfo.DeleteScript(scriptId);
        }


        [JsonRpcMethod("addScript")]
        public int AddScript(ScriptInfoJSON info)
        {
            VerifyAdmin();
            ScriptInfo objConvert = new ScriptInfo();
            //objConvert.ScriptId = info.ScriptId;
            //objConvert.WorkflowId = info.WorkflowId;
            //objConvert.TransitionId = info.TransitionId;
            objConvert.ScriptName = info.ScriptName;
            objConvert.PlacementInt = info.PlacementInt;
            objConvert.Script = info.Script;
            objConvert.EnableTokenReplace = info.EnableTokenReplace == 1 ? true : false;
            objConvert.ModuleId = info.ModuleId;
            objConvert.ScriptOrder = info.ScriptOrder;
            objConvert.ContentType = info.ContentType;

            objConvert.SaveScript();
            return objConvert.ScriptId;
        }

        private void VerifyAdmin()
        {
            PortalSettings ps = PortalController.GetCurrentPortalSettings();
            UserInfo uInfo = UserController.GetCurrentUserInfo();
            if (!uInfo.IsSuperUser && !uInfo.IsInRole(ps.AdministratorRoleName)) throw new Jayrock.JsonRpc.JsonRpcException("This API requires Administrator Permission");
        }


    }
}
